package com.springboot.web.service;

import com.springboot.web.entity.Film;
import com.springboot.web.entity.Rating;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class FilmServiceImpl implements FilmService{

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Film> queryAll() {
        String sql = "SELECT film_id,title,description,release_year,language_id,original_language_id," +
                "rental_duration,rental_rate,length,replacement_cost,rating ,special_features," +
                "last_update FROM film";
        return jdbcTemplate.query(sql,
                (rs,i) ->
                {
                    Integer filmId = rs.getInt("film_id");
                    String title = rs.getString("title");
                    String description = rs.getString("description");
                    String releaseYear = rs.getString("release_year");
                    Integer languageId = rs.getInt("language_id");
                    Integer originalLanguageId = rs.getInt("original_language_id");
                    Integer rentalDuration = rs.getInt("rental_duration");
                    double rentalRate = rs.getDouble("rental_rate");
                    Integer length = rs.getInt("length");
                    double replacementCost = rs.getDouble("replacement_cost");
                    Rating rating = Rating.fromName(rs.getString("rating"));
                    String specialFeaturesValue = rs.getString("special_features");
                    Set<String> specialFeatures = new HashSet<>(Arrays.asList(specialFeaturesValue.split(",")));
                    Timestamp lastUpdate = rs.getTimestamp("last_update");
                    return new Film(filmId,title,description,releaseYear,languageId,originalLanguageId,rentalDuration,rentalRate,length,replacementCost,rating,specialFeatures,lastUpdate);
                });
    }

    @Override
    public Film queryById(Integer id) {
        String sql = "SELECT film_id,title,description,release_year,language_id,original_language_id," +
                "rental_duration,rental_rate,length,replacement_cost,rating ,special_features," +
                "last_update FROM film where film_id = ?";
        return jdbcTemplate.queryForObject(sql,
                (rs,i) ->
                {
                    Integer filmId = rs.getInt("film_id");
                    String title = rs.getString("title");
                    String description = rs.getString("description");
                    String releaseYear = rs.getString("release_year");
                    Integer languageId = rs.getInt("language_id");
                    Integer originalLanguageId = rs.getInt("original_language_id");
                    Integer rentalDuration = rs.getInt("rental_duration");
                    double rentalRate = rs.getDouble("rental_rate");
                    Integer length = rs.getInt("length");
                    double replacementCost = rs.getDouble("replacement_cost");
                    Rating rating = Rating.fromName(rs.getString("rating"));
                    String specialFeaturesValue = rs.getString("special_features");
                    Set<String> specialFeatures = new HashSet<>(Arrays.asList(specialFeaturesValue.split(",")));
                    Timestamp lastUpdate = rs.getTimestamp("last_update");
                    return new Film(filmId,title,description,releaseYear,languageId,originalLanguageId,rentalDuration,rentalRate,length,replacementCost,rating,specialFeatures,lastUpdate);
                },id);
    }

    @Override
    public void addOne(Film film) {
        String insert = "INSERT INTO film VALUES(null,?,?,?,?,?,?,?,?,?,?,?,NOW())";
        String specialFeaturesStr = String.join(",", film.getSpecialFeatures());
        jdbcTemplate.update(insert,
                film.getTitle(),
                film.getDescription(),
                film.getReleaseYear(),
                film.getLanguageId(),
                film.getOriginalLanguageId(),
                film.getRentalDuration(),
                film.getRentalRate(),
                film.getLength(),
                film.getReplacementCost(),
                film.getRating().getName(),
                specialFeaturesStr);
    }

    @Override
    public void modify(Film film) {
        String sql = "update film set title = ?,description = ?,release_year = ?," +
                "language_id = ?,original_language_id = ?,rental_duration = ?,rental_rate = ?," +
                "length = ?,replacement_cost = ?,rating = ?,special_features = ?,last_update = now()" +
                " where film_id = ?";
        String specialFeaturesStr = String.join(",", film.getSpecialFeatures());
        jdbcTemplate.update(sql,
                film.getTitle(),
                film.getDescription(),
                film.getReleaseYear(),
                film.getLanguageId(),
                film.getOriginalLanguageId(),
                film.getRentalDuration(),
                film.getRentalRate(),
                film.getLength(),
                film.getReplacementCost(),
                film.getRating().getName(),
                specialFeaturesStr,
                film.getFilmId());
    }

    @Override
    public void deleteOne(Integer id) {
        String sql = "delete from film where film_id = ? ";
        jdbcTemplate.update(sql,id);
    }
}
